package gui.user;

import database.TMSDatabase;
import database.TableDatabaseUsage;
import info.Flight;
import info.FlightTicket;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import java.awt.*;
import java.util.List;

public class FlightTicketForm extends JFrame {

    private JTable flightTicketTable;
    private String EDPA;
    private String EDST;

    public FlightTicketForm(String EDPA,String EDST){
        super("查询结果");
        setSize(600, 300);
        setLayout(new GridLayout(1,1));

        this.EDPA = EDPA;
        this.EDST = EDST;

        flightTicketTable = new JTable(new FTFModel());
        DefaultTableCellRenderer dc = new DefaultTableCellRenderer();
        dc.setHorizontalAlignment(JLabel.CENTER);
        flightTicketTable.setDefaultRenderer(Object.class,dc);
        JTableHeader dataHeader = flightTicketTable.getTableHeader();
        dataHeader.setPreferredSize(new Dimension(dataHeader.getWidth(), 30));
        dataHeader.setReorderingAllowed(false);
        flightTicketTable.setRowHeight(30);
        JScrollPane sp = new JScrollPane(flightTicketTable);

        add(sp);

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        this.setVisible(true);
    }



    class FTFModel extends AbstractTableModel{
        private String[] columnNames = {"航班号","日期", "舱位", "票价", "余票"};
        private List<FlightTicket> flightTicketList = TableDatabaseUsage.getInstance().getFlightTicketList(EDPA,EDST);


        @Override
        public int getRowCount() {
            return flightTicketList.size();
        }

        @Override
        public int getColumnCount() {
            return columnNames.length;
        }

        @Override
        public String getColumnName(int column) {
            return columnNames[column];
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            FlightTicket dataInfo = flightTicketList.get(rowIndex);
            if(columnIndex == 0) {
                return dataInfo.getFlight();
            } else if(columnIndex == 1) {
                return dataInfo.getFdate();
            } else if(columnIndex == 2) {
                return dataInfo.getFclass();
            } else if(columnIndex == 3) {
                return dataInfo.getPrice();
            } else if(columnIndex == 4) {
                return dataInfo.getTickets();
            }
            return null;
        }
    }
}
